package com.hspedu.newData.dmsxl.数组.双指针;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC844比较含退格的字符串 {

    // 模拟字符串的,也算是快慢指针,不过不符合题意,创建了两个字符串.
    public boolean backspaceCompare(String s, String t) {
        StringBuilder sp =  new StringBuilder();
        StringBuilder tp =  new StringBuilder();
        int slowSp = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '#'){
                if (slowSp != 0){
                    sp.deleteCharAt(--slowSp);
                }
                continue;
            }
            sp.append(s.charAt(i));
            slowSp++;
        }
        int slowTp = 0;
        for (int i = 0; i < t.length(); i++) {
            if (t.charAt(i) == '#'){
                if (slowTp != 0){
                    tp.deleteCharAt(--slowTp);
                }
                continue;
            }
            tp.append(t.charAt(i));
            slowTp++;
        }

        return sp.toString().equals(tp.toString());
    }


    // todo
    // 双指针的方式，快慢指针


    @Test
    public void test() {
        // 测试代码
//        boolean b = backspaceCompare("ad#c", "ad#c");
//        boolean b1 = backspaceCompare("ad##", "a#d#");
//        boolean b2 = backspaceCompare("a#c", "b");
        boolean b3 = backspaceCompare("y#fo##f", "y#f#o##f");
        System.out.println("==========");
    }
}
